<template>
    <div style="width: 100%;height: 100vh;overflow: hidden;background-color: #a4aaff">
        <div style="width: 300px;margin: 100px auto;text-align: center">
            <div style="color: #fdfdff;font-size: 28px;padding: 30px 0">留个号码即可成为作者</div>
            <el-form ref="form" :model="form" :rules="rules" label-width="normal">
                <el-form-item prop="phone">
                    <el-input prefix-icon="el-icon-user-solid" v-model="form.phone"
                              maxLength='11' minlength="11"
                              oninput="value=value.replace(/^\.+|[^\d.]/g,'')" style="background-color: #ffffff"
                              placeholder="请输入号码"></el-input>
                </el-form-item>

                <el-button style="width: 100% ;margin-bottom: 20px;" @click="login">登录</el-button>

            </el-form>
        </div>
    </div>
</template>

<script>
    import request from "../../utils/request";
    import router from "../../router";

    export default {
        name: "registeredAuthor",
        created() {
            request.get("user/grade").then(res => {
                if (res.code === "-1") {
                    router.push(res.data)
                } else if (res.data.role === 0) {
                    router.push("/registeredAuthor")
                } else if (res.data.role === 1) {
                    router.push("/creation")
                }
            })
        },
        data() {
            return {
                form: {},
                rules: {
                    phone: [
                        {required: true, message: '请输入号码', trigger: 'blur'},
                    ],

                }
            }
        },
        methods: {
            login() {
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        if (this.form.phone.length === 11) {
                            request.put("user/registeredAuthor", this.form).then(res => {
                                console.log(res)
                                if (res.code === '0') {
                                    this.$message({
                                        type: "success",
                                        message: "恭喜你成为作者！"
                                    })
                                    this.$router.push("/creation")
                                } else {
                                    this.$message({
                                        type: "error",
                                        message: res.msg
                                    })
                                }
                            })
                        } else {
                            this.$message({
                                type: "error",
                                message: "手机号码必须是11位"
                            })
                        }
                    }
                })

            },


        }
    }
</script>


<style scoped>

</style>